using System;
using System.Collections.Generic;
using System.Text;
using CMS.BIZ;
using System.Web.Security;
using System.Web;
using System.Threading;
using AAM.SQL;
using CMS.BOP;
using AAM.DAL;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Collections.Specialized;
using CMS.Web.UI.UserControls;
using System.Web.Profile;
using System.Configuration.Provider;
using System.Configuration;

namespace CMS.Web.UI
{
    public class Page : System.Web.UI.Page
    {

        static private SqlBizPageProvider bizPageProvider;
        static private SqlBizMembershipProvider bizMembershipProvider;
        static private SqlBizRoleProvider bizRoleProvider;
        static private SqlBizProfileProvider bizProfileProvider;
        static private SqlBizContentProvider bizContentProvider;
        static private SqlBizCultureProvider bizCultureProvider;
        static private SqlBizControlProvider bizControlProvider;
        static private SqlBizMessageProvider bizMessageProvider;
        static private SqlBizMessageFolderProvider bizMessageFolderProvider;
        static private SqlBizMessageStateProvider bizMessageStateProvider;
        static private SqlBizMessageGroupProvider bizMessageGroupProvider;
        static private SqlBizMessageGroupItemProvider bizMessageGroupItemProvider;
        static private SqlBizDownloadProvider bizDownloadProvider;
        static private SqlBizDownloadFolderProvider bizDownloadFolderProvider;
        static private SqlBizForumCategoryProvider bizForumCategoryProvider;
        static private SqlBizForumProvider bizForumProvider;
        static private SqlBizForumMessageProvider bizForumMessageProvider;
        static private SqlBizForumPostProvider bizForumPostProvider;

        #region 'Static Properties'

        static public SqlBizPageProvider BizPageProvider
        {
            get
            {
                if (bizPageProvider == null)
                {
                    bizPageProvider = (SqlBizPageProvider)SiteMap.Provider;
                }
                return bizPageProvider;
            }
        }
        static public SqlBizMembershipProvider BizMembershipProvider
        {
            get
            {
                if (bizMembershipProvider == null)
                {
                    bizMembershipProvider = (SqlBizMembershipProvider)Membership.Provider;
                }
                return bizMembershipProvider;
            }
        }
        static public SqlBizRoleProvider BizRoleProvider
        {
            get
            {
                if (bizRoleProvider == null)
                {
                    bizRoleProvider = (SqlBizRoleProvider)Roles.Provider;
                }
                return bizRoleProvider;
            }
        }
        static public SqlBizProfileProvider BizProfileProvider
        {
            get
            {
                if (bizProfileProvider == null)
                {
                    bizProfileProvider = (SqlBizProfileProvider)ProfileManager.Provider;
                }
                return bizProfileProvider;
            }
        }
        static public SqlBizContentProvider BizContentProvider
        {
            get
            {
                if (bizContentProvider == null)
                {
                    bizContentProvider = (SqlBizContentProvider)BizProviderManager.Instance.GetProvider(BizContent.ObjectType);
                }
                return bizContentProvider;
            }
        }
        static public SqlBizCultureProvider BizCultureProvider
        {
            get
            {
                if (bizCultureProvider == null)
                {
                    bizCultureProvider = (SqlBizCultureProvider)BizProviderManager.Instance.GetProvider(BizCulture.ObjectType);
                }
                return bizCultureProvider;
            }
        }
        static public SqlBizControlProvider BizControlProvider
        {
            get
            {
                if (bizControlProvider == null)
                {
                    bizControlProvider = (SqlBizControlProvider)BizProviderManager.Instance.GetProvider(BizControl.ObjectType);
                }
                return bizControlProvider;
            }
        }
        static public SqlBizMessageProvider BizMessageProvider
        {
            get
            {
                if (bizMessageProvider == null)
                {
                    bizMessageProvider = (SqlBizMessageProvider)BizProviderManager.Instance.GetProvider(BizMessage.ObjectType);
                }
                return bizMessageProvider;
            }
        }
        static public SqlBizMessageFolderProvider BizMessageFolderProvider
        {
            get
            {
                if (bizMessageFolderProvider == null)
                {
                    bizMessageFolderProvider = (SqlBizMessageFolderProvider)BizProviderManager.Instance.GetProvider(BizMessageFolder.ObjectType);
                }
                return bizMessageFolderProvider;
            }
        }
        static public SqlBizMessageStateProvider BizMessageStateProvider
        {
            get
            {
                if (bizMessageStateProvider == null)
                {
                    bizMessageStateProvider = (SqlBizMessageStateProvider)BizProviderManager.Instance.GetProvider(BizMessageState.ObjectType);
                }
                return bizMessageStateProvider;
            }
        }
        static public SqlBizMessageGroupProvider BizMessageGroupProvider
        {
            get
            {
                if (bizMessageGroupProvider == null)
                {
                    bizMessageGroupProvider = (SqlBizMessageGroupProvider)BizProviderManager.Instance.GetProvider(BizMessageGroup.ObjectType);
                }
                return bizMessageGroupProvider;
            }
        }
        static public SqlBizMessageGroupItemProvider BizMessageGroupItemProvider
        {
            get
            {
                if (bizMessageGroupItemProvider == null)
                {
                    bizMessageGroupItemProvider = (SqlBizMessageGroupItemProvider)BizProviderManager.Instance.GetProvider(BizMessageGroupItem.ObjectType);
                }
                return bizMessageGroupItemProvider;
            }
        }
        static public SqlBizDownloadProvider BizDownloadProvider
        {
            get
            {
                if (bizDownloadProvider == null)
                {
                    bizDownloadProvider = (SqlBizDownloadProvider)BizProviderManager.Instance.GetProvider(BizDownload.ObjectType);
                }
                return bizDownloadProvider;
            }
        }
        static public SqlBizDownloadFolderProvider BizDownloadFolderProvider
        {
            get
            {
                if (bizDownloadFolderProvider == null)
                {
                    bizDownloadFolderProvider = (SqlBizDownloadFolderProvider)BizProviderManager.Instance.GetProvider(BizDownloadFolder.ObjectType);
                }
                return bizDownloadFolderProvider;
            }
        }
        static public SqlBizForumCategoryProvider BizForumCategoryProvider
        {
            get
            {
                if (bizForumCategoryProvider == null)
                {
                    bizForumCategoryProvider = (SqlBizForumCategoryProvider)BizProviderManager.Instance.GetProvider(BizForumCategory.ObjectType);
                }
                return bizForumCategoryProvider;
            }
        }
        static public SqlBizForumProvider BizForumProvider
        {
            get
            {
                if (bizForumProvider == null)
                {
                    bizForumProvider = (SqlBizForumProvider)BizProviderManager.Instance.GetProvider(BizForum.ObjectType);
                }
                return bizForumProvider;
            }
        }
        static public SqlBizForumPostProvider BizForumPostProvider
        {
            get
            {
                if (bizForumPostProvider == null)
                {
                    bizForumPostProvider = (SqlBizForumPostProvider)BizProviderManager.Instance.GetProvider(BizForumPost.ObjectType);
                }
                return bizForumPostProvider;
            }
        }
        static public SqlBizForumMessageProvider BizForumMessageProvider
        {
            get
            {
                if (bizForumMessageProvider == null)
                {
                    bizForumMessageProvider = (SqlBizForumMessageProvider)BizProviderManager.Instance.GetProvider(BizForumMessage.ObjectType);
                }
                return bizForumMessageProvider;
            }
        }

        #endregion

        protected Content cMain;

        private bool hasMembershipUser = false;
        private bool hasProfile = false;

        private BizMembershipUser bizMembershipUser = null;
        private BizProfile bizProfile = null;
        private BizPage bizPage = null;

        private string cultureCode = null;

        #region 'Properties'

        public int PageID { get { return bizPage.PageID; } }

        public BizProfile BizProfile 
        { 
            get 
            {
                if (!hasProfile)
                {
                    hasProfile = true;
                    bizProfile = (BizProfile)HttpContext.Current.Profile;
                }
                return bizProfile; 
            } 
        }
        public BizMembershipUser BizMembershipUser 
        { 
            get 
            {
                if (!hasMembershipUser)
                {
                    hasMembershipUser = false;
                    bizMembershipUser = (BizMembershipUser)Membership.GetUser(true);
                }
                return bizMembershipUser; 
            }
        }
        public new MasterPage Master { get { return (MasterPage)base.Master; } }
        public string CultureCode { get { return cultureCode; } }

        #endregion

        public Page()
        {
            base.PreInit += new EventHandler(Page_PreInit);
            base.Init += new EventHandler(Page_Init);
            base.Load += new EventHandler(Page_Load);
            base.Unload += new EventHandler(Page_Unload);
        }

        public string QueryStringPage { get { return "~/?Page=" + PageID; } }
        public string CreateQueryString(NameValueCollection nvl)
        {
            string queryString = "";
            foreach (string key in nvl.AllKeys)
            {
                if (queryString != "") queryString += "&";
                queryString += key + "=" + nvl[key];
            }
            if (queryString != "") return "?" + queryString;
            return "";
        }
        public string ChangeQueryString(string key, string value)
        {
            NameValueCollection nvl = new NameValueCollection(Request.QueryString);
            nvl[key] = value;
            return CreateQueryString(nvl);
        }

        #region 'EventHandlers'

        void Page_PreInit(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["SetCulture"]))
            {
                Session["Culture"] = Request.QueryString["SetCulture"];
            }
            string culture = Request.QueryString["Culture"];
            if (string.IsNullOrEmpty(culture) && Session["Culture"] != null) culture = (string)Session["Culture"];
            if (string.IsNullOrEmpty(culture) && BizMembershipUser != null) culture = BizMembershipUser.CultureCode;
            if (string.IsNullOrEmpty(culture)) culture = ConfigurationManager.AppSettings["defaultCultureCode"];
            if (!string.IsNullOrEmpty(culture))
            {
                cultureCode = culture;
                Culture = culture;
                UICulture = culture;
                Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(culture);
                Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(culture);
            }
            bizPage = (BizPage)SiteMap.Provider.FindSiteMapNode(Request.RawUrl);
            base.Title = bizPage.PageTitle;
            if (!string.IsNullOrEmpty(bizPage.MasterPageFile)) base.MasterPageFile = bizPage.MasterPageFile;
        }

        private void AddContent(int contentId, System.Web.UI.Control control)
        {
            BizContent content = BizContentProvider.Read(contentId, cultureCode);
            if (content != null)
            {
                Literal literal = new Literal();
                literal.Text = content.ContentText;
                control.Controls.Add(literal);
            }
        }

        private void AddControls()
        {
            bool canViewPage = true;
            bool showLoginModule = false;
            if ((bizPage.RequireAuthentication || bizPage.Roles.Count > 0) && (BizMembershipUser == null || !BizMembershipUser.IsValid))
            {
                showLoginModule = true;
                canViewPage = false;
            }
            if (bizPage.Roles.Count > 0 && canViewPage)
            {
                foreach (string role in bizPage.Roles)
                {
                    if (!Roles.IsUserInRole(role))
                    {
                        canViewPage = false;
                        break;
                    }
                }
            }
            //System.Web.UI.Control control = cMain;
            //System.Web.UI.Control control = this;
            System.Web.UI.Control control = null;
            if (Master == null) control = this.Form;
            else control = Master.MainContentPlaceHolder;
            if (control != null)
            {
                if (canViewPage)
                {
                    if (bizPage.ContentID != null) AddContent((int)bizPage.ContentID, control);
                    List<BizControl> list = BizControlProvider.ReadByPageID(bizPage.PageID);
                    foreach (BizControl c in list)
                    {
                        if (c.ContentID != null) AddContent((int) c.ContentID, control);
                        if (!string.IsNullOrEmpty(c.ControlPath)) control.Controls.Add(LoadControl(c.ControlPath));
                    }
                }
                else if (showLoginModule)
                {
                    Login login = new Login();
                    login.DestinationPageUrl = Request.Url.ToString();
                    control.Controls.Add(login);
                }
                else
                {
                    Label label = new Label();
                    label.Text = "Restricted Area";
                    control.Controls.Add(label);
                }
            }
        }

        void Page_Init(object sender, EventArgs e)
        {
            AddControls();
        }

        void Page_Load(object sender, EventArgs e)
        {
        }

        void Page_Unload(object sender, EventArgs e)
        {
            //ConnectionManager.Instance.CloseConnection("defaultConnection");
        }



        #endregion

    }
}
